import { createRouter, createWebHashHistory } from 'vue-router'
import store from '../store/index';

const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/Login.vue')
  }, 
  {
    path: '/',
    redirect: 'index',
    component: () => import('../views/MainBox.vue'),
    children:[
      {
        path:'index',
        component: () => import('../views/home/Home.vue')
      },
      {
          path:'center',
          component: () => import('../views/center/Center.vue')
      },
      {
          path:'user-manage/useradd',
          component: () => import('../views/user-manage/UserAdd.vue'),
          meta:{
            requireAdmin:true
          }
      },
      {
          path:'user-manage/userlist',
          component: () => import('../views/user-manage/UserList.vue'),
          meta:{
            requireAdmin:true
          }
      },
      {
          path:'news-manage/newsadd',
          component: () => import('../views/news-manage/NewsAdd.vue')
      },
      {
          path:'news-manage/newslist',
          component: () => import('../views/news-manage/NewsList.vue')
      },
      {
          path:'news-manage/newsedit/:id',
          component: () => import('../views/news-manage/NewsEdit.vue')
      },
      {
          path:'product-manage/productlist',
          component: () => import('../views/product-manage/ProductList.vue')
      },
      {
          path:'product-manage/productadd',
          component: () => import('../views/product-manage/ProductAdd.vue')
      },      
      {
          path:'product-manage/productedit/:id',
          component: () => import('../views/product-manage/ProductEdit.vue')
      },      
      {
          path:'company-manage/companyadd',
          component: () => import('../views/company-manage/CompanyAdd.vue')
      },      
      {
          path:'carousel-manage/carouseladd',
          component: () => import('../views/carousel-manage/CarouselAdd.vue')
      },      
      {
          path:'carousel-manage/carousellist',
          component: () => import('../views/carousel-manage/CarouselList.vue')
      },      
      {
          path:'cooperation-manage/cooperationadd',
          component: () => import('../views/cooperation-manage/CooperationAdd.vue')
      },      
      {
          path:'cooperation-manage/cooperationlist',
          component: () => import('../views/cooperation-manage/CooperationList.vue')
      },      
      {
          path:'message-feedback/messagelist',
          component: () => import('../views/message-feedback/messageList.vue')
      }, 
      // {
      //   path:'message-feedback/messageedit/:id',
      //   component: () => import('../views/message-feedback/messageEdit.vue')
      // },     
      {
          path:'/:pathMatch(.*)*',
          mame:'Notfound',
          component:()=>import('../views/notfound/Notfound.vue')
      }
    ]
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

const whiteList = ['/login', '/reg'] // 白名单（无需登录可以访问的路由地址）
// 全局前置路由守卫
// 1.浏览器第一次打开项目页面，会触发一次全局前置路由守卫函数
// 2.判断登录与否：有token就证明登录了，无token未登录
// 3.next()如果强制切换路由地址，会再次走路由守卫再次去匹配路由表
router.beforeEach((to,from,next)=>{
  const token=store.state.token
  if(token){
    //判断是否需要授权
    // if(to.meta.requireAdmin && store.state.user.role===1){
    //   console.log(store.state.user);
    //   next()
    // }
   
    next()
  }else{
     // 未登录
    // 数组.includes(值)，作用；判断值是否数组里出现过，出现过原地返回true 
    if(whiteList.includes(to.path)){
      // 未登录，可以访问的路由地址，则放行（路由全局前置守卫不会再次触发了，而是匹配路由表，让组件挂载）
      next()
    }else{
      // next()强制切换到登录路径
      next('/login')
    }
  }
})
export default router
